package com.javaGame.role;

import com.javaGame.frame.MainFrame;
import com.javaGame.role.enemy.EnemyRole;
import com.javaGame.role.enemyBullet.IEnemyBullet;
import com.javaGame.util.ImageChange;
import com.javaGame.util.KeyPress;
import com.sun.tools.javac.Main;

import java.awt.*;

/**
 * 女孩类
 */
public class Girl extends BaseRole{
    /**前进图片数组*/
    public static Image[] forwardImg;
    /**前进无敌图片数组*/
    public static Image[] forwardNotHitImg;
    /**后退图片数组*/
    public static Image[] backImg;
    /**后退无敌图片数组*/
    public static Image[] backNotHitImg;
    /**旋转图片数组*/
    public static Image[] eddyImg;
    /**死亡图片数组*/
    public static Image dieImg;
    /**状态*/
    private int status;
    /**初始化状态常量*/
    public static final int INIT = 0;
    /**前进状态常量*/
    public static final int FORWARD = 1;
    /**后退状态常量*/
    public static final int BACK = 2;
    /**旋转状态常量*/
    public static final int EDDY = 3;
    /**死亡状态常量*/
    public static final int DIE = 4;
    /**是否无敌*/
    private boolean isHit;
    /**无敌计数器*/
    private int hitCount;
    /**炸弹数量*/
    private int bomNum = 3;
    /**旋转计数器*/
    private int eddyCount;
    /**图片切换对象*/
    private ImageChange imageChange = new ImageChange(3);
    /**宠物貂*/
    private  Mink mink;

    public Girl() {
        super(-100, 100, 60, 40);
    }
    public Mink getMink() {
        return mink;
    }

    public int getBomNum() {
        return bomNum;
    }

    @Override
    public void move() {
        if(this.status == INIT){ // 初始状态
            this.setInit();
        }
        else if(this.status == EDDY){// 旋转状态
            this.setEddy();
        }
        else if(this.status == DIE){//死亡状态
            this.setDie();
        }
        else{//普通状态
            this.setMove();
            this.setHit();
            this.setNotHit();
        }

        this.setImage();
    }

    /**
     * 旋转初始化
     */
    public  void eddyInit(){
        if(this.bomNum == 0){//没有炸弹，则不能进入旋转状态
            return;
        }

        //改变状态为旋转
        this.status = EDDY;
        this.width = 30;
        this.height = 45;
        this.bomNum --;

        //产生花瓣
        for(int i=0;i<50;i++){
            MainFrame.roleList.add(new Flower(Flower.RIGHTUP));
            MainFrame.roleList.add(new Flower(Flower.LEFTDOWN));
        }
        //将敌人子弹全部清除
        //将所有敌人的生命减1000
        for(int i=0;i<MainFrame.roleList.size();i++){
            BaseRole role = MainFrame.roleList.get(i);
            if(role instanceof IEnemyBullet){//敌人子弹
                MainFrame.roleList.remove(i);
                i--;
            }
            else if(role instanceof EnemyRole){//敌人
                EnemyRole enemy = (EnemyRole) role;
                enemy.byHit(1000);
                if(enemy.getHp() <= 0){
                    i--;
                }
            }
        }


    }

    /**
     * 设置图片
     */
    private void setImage(){
        if(this.status == FORWARD && this.isHit == true){//前进无敌状态
            this.currentImg = imageChange.change(forwardNotHitImg);
        }
        else if(this.status == FORWARD && this.isHit == false){//前进普通状态
            this.currentImg = imageChange.change(forwardImg);
        }
        else if(this.status == BACK && this.isHit == true){//后退无敌状态
            this.currentImg = imageChange.change(backNotHitImg);
        }
        else if(this.status == BACK && this.isHit == false){//后退普通状态
            this.currentImg = imageChange.change(backImg);
        }
        else if(this.status == EDDY){//旋转
            this.currentImg = imageChange.change(eddyImg);
        }
        else if(this.status == DIE){//死亡
            this.currentImg = dieImg;
        }
        else if(this.status == INIT){//初始状态
            this.currentImg = imageChange.change(forwardImg);
        }

        if(this.status == FORWARD){
            this.width = 60;
            this.height = 40;
        }
        else if(this.status == BACK){
            this.width = 40;
            this.height = 60;
        }
    }

    /**
     * 设置无敌
     */
    private void setNotHit(){
        if(isHit == false){//普通状态跳转
            return;
        }

        this.hitCount++;
        if(this.hitCount > 150){//3秒以后，退出无敌状态
            this.hitCount = 0 ;
            this.isHit = false;
        }
    }

    /**
     *  设置碰撞
     */
    private void setHit(){
        for(int i=0;i<MainFrame.roleList.size();i++){
            BaseRole role = MainFrame.roleList.get(i);

            if(this.rect.intersects(role.rect) && isHit == false
                && (role instanceof EnemyRole || role instanceof IEnemyBullet)){
                this.status = DIE;

                if(this.mink != null){//宠物不为空
                    if(this.mink.getFire() != null){//火不为空，则移除火
                        MainFrame.roleList.remove(this.mink.getFire());
                    }
                    MainFrame.roleList.remove(this.mink);
                }

            }
            else if(this.rect.intersects(role.rect) && role instanceof Prop){//主角捡道具
                MainFrame.pointer += 1000;
                Prop prop = (Prop)role;
                if(prop.getType() == Prop.ADDLIFE){//加人道具
                    MainFrame.girlNum ++;
                }
                else if(prop.getType() == Prop.BOMB){//炸弹
                    this.bomNum ++;
                }
                else if(prop.getType() == Prop.FIRE){//火力
                    if(this.mink == null){
                        this.mink = new Mink();
                        MainFrame.roleList.add(this.mink);
                    }
                }
                MainFrame.roleList.remove(prop);
            }
        }
    }

    /**
     * 设置移动
     */
    private void setMove(){
        if(KeyPress.isUp == true){
            this.y -= 5;
            if(this.y <= 30){
                this.y = 30;
            }
        }
        if(KeyPress.isDown == true){
            this.y += 5;
            if(this.y >= 660){
                this.y = 660;
            }
        }
        if(KeyPress.isLeft == true){
            this.status = BACK;
            this.x -= 5;
            if(this.x <= 0){
                this.x = 0;
            }
        }
        if(KeyPress.isRight == true){
            this.status = FORWARD;
            this.x += 5;
            if(this.x >= 940){
                this.x = 940;
            }
        }
        if (KeyPress.isLeft == false && KeyPress.isRight == false){
            this.status = FORWARD;
        }
    }

    /**
     * 设置死亡
     */
    private void setDie(){
        this.y += 15;
        if(this.y >= 800){
            MainFrame.girlNum --;

            if(MainFrame.girlNum > 0 ){//产生新的主角
                //从集合中移除当前女孩
                MainFrame.roleList.remove(this);
                //产生新的主角对象
                MainFrame.girl = new Girl();
                //将新的主角对象加入角色集合
                MainFrame.roleList.add(MainFrame.girl);
            }
            else{//通关失败，结束游戏
                MainFrame.isPass = false;
                MainFrame.isRun = false;
            }
        }
    }

    /**
     * 设置旋转
     */
    private void setEddy(){
        this.eddyCount++;
        if(this.eddyCount <= 40){
            this.width +=4;
            this.height += 4;
        }
        else if(this.eddyCount <= 80){
            this.width -= 4;
            this.height -= 4;
        }
        else{
            this.status = FORWARD;
            this.isHit = true;
            this.eddyCount = 0;
        }
    }

    /**
     * 设置初始状态
     */
    private void setInit() {
        this.x += 8;
        if(this.x > 100){
            this.status = FORWARD;
            this.isHit = true;
        }
    }

    public int getStatus() {
        return status;
    }
}
